# -*- coding: utf-8 -*-
"""
自定义管道组件
"""

import os
import ppc
import ppc.pipe
import psycopg2


class NovelClean(ppc.pipe.Pipe):
    """删除采集的[通知]章节"""

    def handle(self):
        config = {
            'host': self['host'].value,
            'port': self['port'].value,
            'database': self['database'].value,
            'user': self['user'].value,
            'password': self['password'].value
        }

        conn = psycopg2.connect(**config)
        cur = conn.cursor()

        where = "postdate>now()-interval'10 minute' AND chaptername='本站重要通告'"
        cur.execute("SELECT articleno,chapterno FROM t_chapter WHERE {0}".format(where)) 
        rows = cur.fetchall()
        if rows:
            for r in rows:
                path = '/usr/local/tomcat/webapps/ROOT/txt/%s/%s/%s.txt' % (r[0] // 1000, r[0], r[1])
                print(path)
                os.remove(path)
            cur.execute("DELETE FROM t_chapter WHERE {0}".format(where))
            conn.commit()

        conn.close()
